package com.zq.qingzhu.controller.mall;

import com.zq.qingzhu.bean.GoodsDetail;
import com.zq.qingzhu.common.Constants;
import com.zq.qingzhu.controller.vo.GoodsDetailVO;
import com.zq.qingzhu.controller.vo.SearchPageCategoryVO;
import com.zq.qingzhu.service.CategoryService;
import com.zq.qingzhu.service.GoodsDetailService;
import com.zq.qingzhu.util.BeanUtil;
import com.zq.qingzhu.util.PageQueryUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class MallGoodsController {

	@Resource
	private GoodsDetailService goodsDetailService;
	@Resource
	private CategoryService categoryService;

	@GetMapping({"/search", "/search.html"})
	public String searchPage(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		if (StringUtils.isEmpty(params.get("page"))) {
			params.put("page", 1);
		}
		params.put("limit", Constants.GOODS_SEARCH_PAGE_LIMIT);
		//封装分类数据
		if (params.containsKey("goodsCategoryId") && !StringUtils.isEmpty(params.get("goodsCategoryId") + "")) {
			Integer categoryId = Integer.valueOf(params.get("goodsCategoryId") + "");
			SearchPageCategoryVO searchPageCategoryVO = categoryService.getCategoriesForSearch(categoryId);
			if (searchPageCategoryVO != null) {
				request.setAttribute("goodsCategoryId", categoryId);
				request.setAttribute("searchPageCategoryVO", searchPageCategoryVO);
			}
		}
		//封装参数供前端回显
		if (params.containsKey("orderBy") && !StringUtils.isEmpty(params.get("orderBy") + "")) {
			request.setAttribute("orderBy", params.get("orderBy") + "");
		}
		String keyword = "";
		//对keyword做过滤 去掉空格
		if (params.containsKey("keyword") && !StringUtils.isEmpty((params.get("keyword") + "").trim())) {
			keyword = params.get("keyword") + "";
		}
		request.setAttribute("keyword", keyword);
		params.put("keyword", keyword);
		//封装商品数据
		PageQueryUtil pageUtil = new PageQueryUtil(params);
		request.setAttribute("pageResult", goodsDetailService.searchGoods(pageUtil));
		return "mall/search";
	}

	@GetMapping("/goods/detail/{goodsId}")
	public String detailPage(@PathVariable("goodsId") Integer goodsId, HttpServletRequest request) {
		if (goodsId < 1) {
			return "error/error_5xx";
		}
		GoodsDetail goods = goodsDetailService.getGoodsById(goodsId);
		if (goods == null) {
			return "error/error_404";
		}
		GoodsDetailVO goodsDetailVO = new GoodsDetailVO();
		BeanUtil.copyProperties(goods, goodsDetailVO);
		goodsDetailVO.setGoodsId(goods.getId());
		goodsDetailVO.setGoodsDescription(goods.getDescription());
//		goodsDetailVO.setGoodsCarouselList(goods.getGoodsCarousel().split(","));
		request.setAttribute("goodsDetail", goodsDetailVO);
//		return "mall/detail";
		return "qingzhu/front/detail";
	}

}
